package cn.shop.dys.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import cn.shop.dys.bean.productdetail.ProductData;
import cn.shop.dys.bean.productdetail.ProductDetail;
import cn.shop.dys.constant.DetaiConstants;
import cn.shop.dys.R;

//import fei.itcast.cn.productdetails.bean.Product;

/**
 * Created by Administrator on 2016/4/21.
 */
public class ProductDetailsFragment extends Fragment {

    private TextView productDetail;
    private int pId;
    private ProductDetail product;
    private String url;
    private View rootView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_product_details, null);
        productDetail = (TextView) rootView.findViewById(R.id.fragment_texttitle);

        pId = getActivity().getIntent().getIntExtra("pId", pId);
        url = DetaiConstants.HOST+"/product?pId="+ pId;

        return rootView;
    }

    private void downLoadData() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET,url,callBack);
    }

    RequestCallBack<String> callBack =  new RequestCallBack<String>() {
        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            String result = responseInfo.result;
            Log.i("result",""+result);
            Gson gson = new Gson();
            ProductData productData = gson.fromJson(result, ProductData.class);
            Log.i("product"," "+productData);
            product = productData.getProduct();

            String productDesc = product.getProductDesc();
            productDetail.setText(productDesc);
        }

        @Override
        public void onFailure(HttpException e, String s) {
            Log.e("onFailure", "失败了");
        }
    };

}
